#!/bin/sh

# ANSI COLORS
CRE="
[K"
NORMAL="[0;39m"
RED="[1;31m"
GREEN="[1;32m"
YELLOW="[1;33m"
BLUE="[1;34m"
MAGENTA="[1;35m"
CYAN="[1;36m"
WHITE="[1;37m"

export args=$1

# 设置ssh，首次进入系统需要添加用户
messagebus_user=`cat /etc/passwd | grep "messagebus"`
echo $messagebus_user
if [ -z "$messagebus_user" ]; then
    echo "messagebus:x:1000:1000:Linux User,,,:/home/messagebus:/bin/sh" >> /etc/passwd
fi

if [ ! -f "/var/run/dbus/pid" ]; then
	rm -f /var/run/dbus/pid
fi

if test ! -f /usb/lib/libnl-3.so.200; then
	ln -s /usr/lib/libdbus-1.so.3.19.13  /usr/lib/libdbus-1.so.3
fi

if test ! -f /usb/lib/libexpat.so.1; then
	ln -s /usr/lib/libexpat.so.1.8.6  /usr/lib/libexpat.so.1
fi

if test ! -f /usb/lib/libglib-2.0.so.0; then
	ln -s /usr/lib/libglib-2.0.so.0.4000.0  /usr/lib/libglib-2.0.so.0
fi

if test ! -f /usb/lib/libreadline.so.8; then
	ln -s /usr/lib/libreadline.so.8.1  /usr/lib/libreadline.so.8
fi

function ble_start()
{
	# 如果plat_soc.ko没有加载，那么就加载
	plat_soc=`lsmod | grep "plat_soc"`
	if [ -z "$plat_soc" ]; then 
		insmod /ko/wifi/plat_soc.ko
	fi

	# 如果ble_soc.ko没有加载，那么就加载
	ble_soc=`lsmod | grep "ble_soc"`
	if [ -z "$ble_soc" ]; then 
		insmod /ko/wifi/ble_soc.ko
	fi

	sleep 3
	dbus_result=`dbus-daemon --config-file=/usr/share/dbus-1/session.conf --print-address --fork`
	export DBUS_SESSION_BUS_ADDRESS=$dbus_result
	export DBUS_SYSTEM_BUS_ADDRESS=$dbus_result

	bluetoothd -n &

	sleep 2

	echo "${CYAN}将以下两行内容复制到终端并执行${NORMAL}"
	echo "${GREEN}export DBUS_SESSION_BUS_ADDRESS=$dbus_result${NORMAL}"
	echo "${GREEN}export DBUS_SYSTEM_BUS_ADDRESS=$dbus_result${NORMAL}"
}

function ble_stop()
{
	killall dbus-daemon
	killall bluetoothd
	sleep 1;
	rmmod ble_soc.ko
}

case $args in
	start)
		ble_start
		;;
	stop)
		ble_stop
		;;
	-h)
		echo 'Options: start stop '
		;;
	*)
		echo 'Use '-h' to display command line options of sub-processes'
		;;
esac
